package com.ztewatch.wearcontacts.view;

import java.io.InputStream;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ztewatch.wearcontacts.R;
import com.ztewatch.wearcontacts.data.SortItem;
import com.ztewatch.wearcontacts.utils.ContactPhotoUtils;

public class SortGridViewAdapter extends BaseAdapter {
	private List<SortItem> list;
	private LayoutInflater mInflater;
	private Context mContext;
	private boolean voice_call_flag = false;

	public SortGridViewAdapter(Context context, List<SortItem> list) {
		this.list = list;
		this.mContext = context;
		mInflater = LayoutInflater.from(context);
	}

	public void setVoiceCallFlag(boolean isVoiceCall) {
		voice_call_flag = isVoiceCall;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.grid_item, null);
			holder = new ViewHolder();
			holder.mTextView = (TextView) convertView
					.findViewById(R.id.item_text);
			holder.mImageView = (ImageView) convertView
					.findViewById(R.id.item_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// holder.mImageView.setImageResource(R.drawable.zte_contact_picture_holo_light);

		if (list.get(position).getIs_group() == 0) {
			holder.mTextView.setText(list.get(position).getName());
			long photoid = list.get(position).getPhotoId();
			long contactid = list.get(position).getContact_id();
			bindContactPhoto(holder.mImageView, photoid, contactid);
		} else {
			holder.mTextView.setText(list.get(position).getGroup_title());
			bindGroupPhoto(holder.mImageView, list.get(position)
					.getGroup_photo_arr());
		}
//		if (position == 0
//				&& mContext.getResources().getString(R.string.history_str)
//						.equals(list.get(position).getName())
//				&& !voice_call_flag) {
//			holder.mImageView.setImageResource(R.drawable.ic_history);
//			holder.mImageView.setEnabled(false);
//			holder.mTextView.setEnabled(false);
//		} else {
//			holder.mImageView.setEnabled(true);
//			holder.mTextView.setEnabled(true);
//		}
		return convertView;
	}

	private void bindContactPhoto(ImageView photo, long photoId, long contactId) {
		if (photoId != 0) {
			Uri uri = ContentUris.withAppendedId(
					ContactsContract.Contacts.CONTENT_URI, contactId);
			InputStream input = ContactsContract.Contacts
					.openContactPhotoInputStream(mContext.getContentResolver(),
							uri);
			photo.setImageBitmap(BitmapFactory.decodeStream(input));
		} else {
			photo.setImageResource(R.drawable.ic_address_portrait);
		}
	}

	private void bindGroupPhoto(ImageView photo, byte[] photoarr) {
		// int photo_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
		if (photoarr != null) {
			Bitmap bitmap = ContactPhotoUtils.Bytes2Bimap(photoarr);
			photo.setImageBitmap(bitmap);
		} else {
			photo.setImageResource(R.drawable.ic_friends);
		}
	}

	private class ViewHolder {
		TextView mTextView;
		ImageView mImageView;
	}
}
